//
//  PYVideoPlayer.m
//  catuncel
//
//  Created by Jim on 2017/12/13.
//  Copyright © 2017年 Jim. All rights reserved.
//

#import "PYVideoPlayer.h"

@interface PYVideoPlayer () <JPVideoPlayerDelegate>

@property (nonatomic, strong) UIView *containerView;

@end

@implementation PYVideoPlayer

+ (instancetype)sharedInstance
{
    static PYVideoPlayer *player = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        player = [[PYVideoPlayer alloc] init];
        
    });
    
    return player;
}

-(void)playStoreWithUrl:(NSURL *)url showView:(UIView *)showView
{
    _containerView = showView;
    _url = [url.path copy];
    [showView jp_playVideoWithURL:url options:JPVideoPlayerContinueInBackground | JPVideoPlayerShowProgressView | JPVideoPlayerLayerVideoGravityResizeAspectFill | JPVideoPlayerRetryFailed progress:nil completed:^(NSString * _Nullable fullVideoCachePath, NSError * _Nullable error, JPVideoPlayerCacheType cacheType, NSURL * _Nullable videoURL) {
        
    }];
    
}

- (void)playWithUrl:(NSString *)url showView:(UIView *)containerView newsId:(NSString *)newsId
{
    [self playWithUrl:url showView:containerView mute:_mute newsId:newsId aspectFit:NO];
    
}

- (void)playWithLocalUrl:(NSURL *)localUrl showView:(UIView *)containerView
{
    _containerView = containerView;
    
    _url = [localUrl.path copy];
    
    NSLog(@"正在播放url:%@", _url);
    
    [containerView jp_playVideoWithURL:localUrl options:JPVideoPlayerContinueInBackground | JPVideoPlayerShowProgressView | JPVideoPlayerLayerVideoGravityResizeAspectFill | JPVideoPlayerRetryFailed progress:nil completed:nil];
}

- (void)playWithUrl:(NSString *)url showView:(UIView *)containerView mute:(BOOL)mute newsId:(NSString *)newsId aspectFit:(BOOL)aspectFit
{
    _containerView = containerView;
    
    _containerView.jp_videoPlayerDelegate = self;
    
    _url = [url copy];
    
    NSLog(@"正在播放url:%@", url);
    
    __weak typeof(self) weakSelf = self;

    if (mute) {
        
        if (aspectFit) {
            
            [containerView jp_playVideoWithURL:[url qiniuVideoUrl] options:JPVideoPlayerContinueInBackground | JPVideoPlayerShowProgressView | JPVideoPlayerLayerVideoGravityResizeAspect | JPVideoPlayerMutedPlay | JPVideoPlayerRetryFailed progress:nil completed:^(NSString * _Nullable fullVideoCachePath, NSError * _Nullable error, JPVideoPlayerCacheType cacheType, NSURL * _Nullable videoURL) {
                
                [weakSelf calculatePlayTimesWithNewsId:newsId];
            }];
            
        } else {
            
            [containerView jp_playVideoWithURL:[url qiniuVideoUrl] options:JPVideoPlayerContinueInBackground | JPVideoPlayerShowProgressView | JPVideoPlayerLayerVideoGravityResizeAspectFill | JPVideoPlayerMutedPlay | JPVideoPlayerRetryFailed progress:nil completed:^(NSString * _Nullable fullVideoCachePath, NSError * _Nullable error, JPVideoPlayerCacheType cacheType, NSURL * _Nullable videoURL) {
                
                [weakSelf calculatePlayTimesWithNewsId:newsId];
            }];
        }

    
    } else {
        
        if (aspectFit) {
            
            [containerView jp_playVideoWithURL:[url qiniuVideoUrl] options:JPVideoPlayerContinueInBackground | JPVideoPlayerShowProgressView | JPVideoPlayerLayerVideoGravityResizeAspect | JPVideoPlayerRetryFailed progress:nil completed:^(NSString * _Nullable fullVideoCachePath, NSError * _Nullable error, JPVideoPlayerCacheType cacheType, NSURL * _Nullable videoURL) {
                
                [weakSelf calculatePlayTimesWithNewsId:newsId];
                
            }];
            
        } else {
            
            [containerView jp_playVideoWithURL:[url qiniuVideoUrl] options:JPVideoPlayerContinueInBackground | JPVideoPlayerShowProgressView | JPVideoPlayerLayerVideoGravityResizeAspectFill | JPVideoPlayerRetryFailed progress:nil completed:^(NSString * _Nullable fullVideoCachePath, NSError * _Nullable error, JPVideoPlayerCacheType cacheType, NSURL * _Nullable videoURL) {
                
                [weakSelf calculatePlayTimesWithNewsId:newsId];
                
            }];
        }

    }
}

- (void)calculatePlayTimesWithNewsId:(NSString *)newsId
{
    if (newsId) {
                
//        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
//
//        parameters[@"userId"] = [NSString valueByKey:kCurrentUserID];
//
//        parameters[@"newsId"] = newsId;
//        
//        [PYNetworking networkingWithURLString:postVideoPlayTimesURL parameters:parameters requestType:JZ_POST success:^(NSURLSessionDataTask *task, id responseObject) {
//
//        } failure:^(NSURLSessionDataTask *task, NSError *error) {
//
//        }];
    }
}

- (void)stop
{
    [_containerView jp_stopPlay];
    
    NSLog(@"停止播放url:%@", _url);

    _url = nil;
}

- (void)pause
{
    _pauseUrl = _url;
    
    [_containerView jp_pause];
}

- (void)resumeWithContainerView:(UIView *)containerView url:(NSString *)url
{
    if ([_url isEqualToString:_pauseUrl]) {
        
        [_containerView jp_resume];

    } else {
        
        [self playWithUrl:url showView:containerView mute:NO newsId:nil aspectFit:NO];

    }
}

- (void)setMute:(BOOL)mute
{
    _mute = mute;
    
    [_containerView jp_setPlayerMute:mute];
}

- (BOOL)shouldAutoReplayAfterPlayCompleteForURL:(nonnull NSURL *)videoURL;
{
    return !self.cancelAutoReplay;
}

@end
